package com.excuse.excusebuilder.commen.util;

import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

import com.excuse.excusebuilder.impl.internet.WifiConnectedListener;

/**
 * Created by U-nookia on 2017/3/20.
 */

public class NetWorkUtil {
    public static boolean netWorkConnected;

    /*
   判断是否有连接可用的网络
    */
    public static boolean isNetWorkConnected(Context context,WifiConnectedListener listener){
        if (context==null) {
            NetWorkUtil.netWorkConnected = false;
            return false;
        }
        ConnectivityManager manager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo activeNetwork = manager.getActiveNetworkInfo();
        if (activeNetwork==null||!activeNetwork.isConnected()){
            NetWorkUtil.netWorkConnected = false;
            ToastUtil.makeShortToast(context,"请检查网络");
            return false;
        }
        if (activeNetwork.getType() == ConnectivityManager.TYPE_MOBILE){
            if (activeNetwork.isConnected()){
                NetWorkUtil.netWorkConnected = true;
                return true;
            }
        }
        if (activeNetwork.getType() == ConnectivityManager.TYPE_WIFI) {
            if (activeNetwork.isConnected()){
                //initChectInternetRetrofit(context,listener);
            }else {
                NetWorkUtil.netWorkConnected = false;
                ToastUtil.makeShortToast(context,"网络断开，请检查网络连接");
                return false;
            }
        }
        return false;
    }

    /*private static void initChectInternetRetrofit(final Context context, final WifiConnectedListener listener) {
        String url = "http://www.tngou.net";
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(url)
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        CheckInternetService service = retrofit.create(CheckInternetService.class);
        Call<InternetTestTngou> call = service.getList("cook",0, 1, 20);
        call.enqueue(new Callback<InternetTestTngou>() {
            @Override
            public void onResponse(Call<InternetTestTngou> call, Response<InternetTestTngou> response) {
                if (response.isSuccessful()){
                    NetWorkUtil.netWorkConnected = true;
                    listener.wifiConnected();
                    //ToastUtil.makeShortToast(context,"wifi已连接");
                }else {
                    NetWorkUtil.netWorkConnected = false;
                    ToastUtil.makeShortToast(context,"网络无法使用");
                }
            }

            @Override
            public void onFailure(Call<InternetTestTngou> call, Throwable t) {
                NetWorkUtil.netWorkConnected = false;
                ToastUtil.makeShortToast(context,"网络无法使用");
            }
        });
    }*/
}
